<template>
  <div class="app-content">
    <div class="app-form">
      <Form ref="formValidate" :label-width="80">
        <FormItem label="管控结果" required>
          <RadioGroup v-model="model.controlStatus">
            <Radio label="3">到位</Radio>
            <Radio label="2">不到位</Radio>
          </RadioGroup>
        </FormItem>
        <FormItem label="管控说明">
          <Input
            v-model="model.controlDescription"
            type="textarea"
            maxlength="500"
            show-word-limit
            :rows="7"
            placeholder="请输入"
          ></Input>
        </FormItem>
        <FormItem label="照片">
          <div
            class="demo-upload-list"
            v-for="(item, index) in uploadList"
            :key="index"
          >
            <template v-if="item.status === 'finished'">
              <img :src="item.url" />
              <div class="demo-upload-list-cover">
                <Icon
                  type="ios-eye-outline"
                  @click.native="handleView(item.url)"
                ></Icon>
                <Icon
                  type="ios-trash-outline"
                  @click.native="handleRemove(item)"
                ></Icon>
              </div>
            </template>
            <template v-else>
              <Progress
                v-if="item.showProgress"
                :percent="item.percentage"
                hide-info
              ></Progress>
            </template>
          </div>
          <Upload
            ref="upload"
            v-show="this.uploadList.length < 5"
            :show-upload-list="false"
            :on-success="handleSuccess"
            :format="imageTypes"
            :max-size="maxSize"
            :on-format-error="handleFormatError"
            :on-exceeded-size="handleMaxSize"
            :before-upload="handleBeforeUpload"
            multiple
            type="drag"
            :action="uploadUrl"
            style="display: inline-block;width:58px;"
          >
            <div style="width: 58px;height:58px;line-height: 58px;">
              <Icon type="ios-camera" size="20"></Icon>
            </div>
          </Upload>
        </FormItem>
        <FormItem label="附件" prop="desc">
          <Upload
            multiple
            ref="annexUpload"
            :action="uploadUrl"
            :format="annexTypes"
            :max-size="maxSize"
            :on-format-error="handleFormatError"
            :on-exceeded-size="handleMaxSize"
            :before-upload="handleAnnexBeforeUpload"
            :on-success="handleFileSuccess"
            :on-remove="handleFileRemove"
          >
            <Button v-show="uploadButtonShow" icon="ios-cloud-upload-outline"
              >上传文件</Button
            >
          </Upload>
        </FormItem>
        <FormItem>
          <Button type="primary" @click="save">保存</Button>
          <Button @click="cancel" style="margin-left: 8px">取消</Button>
        </FormItem>
      </Form>
    </div>
    <Modal title="图片预览" v-model="visible">
      <img
        :src="previewImage"
        v-if="visible"
        style="width: 100%; height: 300px;"
      />
    </Modal>
    <Modal v-model="isInquiries" title="业务问询" width="50%" ok-text="提交" :footer-hide="true">
      <div class="modal-content">
        <inquiries ref="isInquiriesBox" :checkedRows="checkedRows" :businessType="businessType" @modelBox="modelBox" @person="person"></inquiries>
      </div>
    </Modal>
    <Modal v-model="personModel" title="选择人" width="80%" ok-text="提交" :footer-hide="true">
      <div class="modal-content">
        <checkPerson @personBox="personBox"></checkPerson>
      </div>
    </Modal>
    <span class="inquiries" @click="replyChild()">问询</span>
  </div>
</template>

<script>
import { mapMutations } from 'vuex'
import inquiries from '../../../components/modules/inquiries/index'
import checkPerson from '../../../components/modules/inquiries/checkPerson'
export default {
  components:{
      inquiries,
      checkPerson
    },
  name: 'procedure-control-edit',
  props: ['id'],
  data() {
    return {
      checkedRows:[],
      businessType:'businesstype_control',
      isInquiries:false,
      personModel:false,
      maxSize: 5 * 1024,
      imageTypes: [
        'jpg',
        'jpeg',
        'png',
        'png',
        'bmp',
        'tif',
        'gif',
        'pjpeg',
        'jfif',
        'pjp'
      ],
      annexTypes: [
        'doc',
        'docx',
        'ppt',
        'txt',
        'pdf',
        'xls',
        'xlsx',
        'zip',
        'rar'
      ],
      imgName: '',
      visible: false,
      uploadList: [],
      fileServer: '',
      uploadUrl: '',
      previewUrl: '',
      previewImage: '',
      model: {
        id: this.id,
        controlStatus: '',
        controlDescription: '',
        controlPictures: '',
        attachment: ''
      },
      uploadButtonShow: true
    }
  },
  methods: {
      replyChild(){
        if(this.checkedRows.length > 5){
          this.$Message.warning('最多可选5条措施进行批量问询')
          return 
        }else if(this.checkedRows.length == 0){
            this.$Message.warning('请先选择需要问询的措施')
            return
        }else{
          this.isInquiries = true
          this.$refs.isInquiriesBox.isModel()
        }
      },
      // 关闭问询框
      modelBox(){
        this.isInquiries = false
      },
      // 打开人员选择框
      person(){
        this.personModel = true
      },
      // 关闭人员选择框
      personBox(){
        this.personModel = false
      },
      initRiskData() {
      this.$api.processControl
        .riskDetail(this.$route.params.id)
        .then(({ code, data, message }) => {
          200 === code ? (this.checkedRows.push(data)) : this.$Message.error(message)
        })
    },
    ...mapMutations(['closeTag']),
    handleView(url) {
      this.previewImage = url
      this.visible = true
    },
    handleRemove(file) {
      const fileList = this.$refs.upload.fileList
      this.$refs.upload.fileList.splice(fileList.indexOf(file), 1)
    },
    handleSuccess({ statusCode, fileIds, message }, file) {
      if (200 === statusCode) {
        file.url = `${this.previewUrl}${fileIds}`
        file.id = fileIds
      } else {
        this.$Message.error(message)
      }
    },
    handleFormatError(file) {
      this.$Message.error(`${file.name}格式不正确`)
    },
    handleMaxSize(file) {
      this.$Message.error(`${file.name}大小超过5m`)
    },
    handleBeforeUpload() {
      const check = this.uploadList.length < 5
      if (!check) {
        this.$Message.error('最多上传5个文件')
      }
      return check
    },
    handleAnnexBeforeUpload() {
      const check = this.$refs.annexUpload.fileList.length < 5
      if (!check) {
        this.$Message.error('最多上传5个文件')
      }
      return check
    },
    handleFileSuccess(response, file, fileList) {
      this.uploadButtonShow = fileList.length < 5
    },
    // eslint-disable-next-line no-unused-vars
    handleFileRemove(file, fileList) {
      this.uploadButtonShow = true
    },
    save() {
      if (!this.model.controlStatus) {
        this.$Message.error('请选择管控结果')
        return
      }
      this.model.controlPictures = this.uploadList
        .map(image => image.id)
        .join(',')
      this.model.attachment = this.$refs.annexUpload.fileList
        .map(annex => annex.response.fileIds)
        .join(',')
      this.$api.processControl.control(this.model).then(({ code, message }) => {
        if (200 === code) {
          this.$Message.success('管控成功')
          this.closeTag({
            name: this.$route.name,
            params: { ...this.$route.params },
            query: { ...this.$route.query }
          })
          this.$router.push({
            name: 'procedure-control'
          })
        } else {
          this.$Message.error(message)
        }
      })
    },
    cancel() {
      this.closeTag({
        name: this.$route.name,
        params: { ...this.$route.params },
        query: { ...this.$route.query }
      })
    },
    init() {
      this.fileServer = process.env.VUE_APP_FILE_SERVER_URL
      this.uploadUrl = `${this.fileServer}file/uploadFile`
      this.previewUrl = `${this.fileServer}file/downFile/`
    }
  },
  mounted() {
    this.init()
    this.initRiskData()
    this.uploadList = this.$refs.upload.fileList
  }
}
</script>

<style lang="scss" scoped>
.demo-upload-list {
  display: inline-block;
  width: 60px;
  height: 60px;
  text-align: center;
  line-height: 60px;
  border: 1px solid transparent;
  border-radius: 4px;
  overflow: hidden;
  background: #fff;
  position: relative;
  box-shadow: 0 1px 1px rgba(0, 0, 0, 0.2);
  margin-right: 4px;
}
.demo-upload-list img {
  width: 100%;
  height: 100%;
}
.demo-upload-list-cover {
  display: none;
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  background: rgba(0, 0, 0, 0.6);
}
.demo-upload-list:hover .demo-upload-list-cover {
  display: block;
}
.demo-upload-list-cover i {
  color: #fff;
  font-size: 20px;
  cursor: pointer;
  margin: 0 2px;
}
</style>
